package DAO.impl;

import DAO.BaseDao;
import DAO.UserDAO;
import bean.Users;

import java.sql.Connection;
import java.util.List;

/**
 * @author 栋
 * @date 2022/11/8
 * @description
 */
public class UserDAOImpl extends BaseDao<Users> implements UserDAO {
    @Override
    public int add_user(Connection conn, Users user) {
        String sql="insert into users(user_id,phone_number,user_name,user_pwd,sex,introduce,stu_number,credit_number,address) values(?,?,?,?,?,?,?,?,?)";
        return update(conn,sql,user.getUser_id(),user.getPhone_number(),user.getUser_name(),user.getUser_pwd(),user.getSex(),user.getIntroduce(),user.getStu_number(),user.getCredit_number(),user.getAddress());
    }

    @Override
    public int update_user(Connection conn, Users user) {
        String sql="update users set phone_number=?,user_name=?,user_pwd=?,sex=?,introduce=?,stu_number=?,credit_number=?,address=? where user_id=?";
        return update(conn,sql,user.getPhone_number(),user.getUser_name(),user.getUser_pwd(),user.getSex(),user.getIntroduce(),user.getStu_number(),user.getCredit_number(),user.getAddress(),user.getUser_id());
    }

    @Override
    public int delete_user(Connection conn, String user_id) {
        String sql="delete from users where user_id=?";
        return update(conn,sql,user_id);
    }

    @Override
    public List<Users> get_all_users(Connection conn) {
        String sql="select * from users";
        return getForList(conn,sql);
    }

    @Override
    public Users getUserById(Connection conn, String user_id) {
        String sql="select *from users where user_id=?";
        return getInstance(conn,sql,user_id);
    }

    @Override
    public Users getUserByPhone(Connection conn, String phoneNumber) {
        String sql="select* from users where phone_number=?";
        return getInstance(conn,sql,phoneNumber);
    }

    @Override
    public Users getUserByNumber(Connection conn, String number) {
        String sql="select * from users where stu_number=?";
        return getInstance(conn,sql,number);
    }
}
